import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-03-01
 * Time: 12:45
 */
public class test2 {
    /**
     * LeetCode  字符串解码
     */

    // 遇到数字怎么办，字母怎么办，左括号怎么办，右括号怎么办
    public String decodeString(String s) {
        // 数字栈
        Stack<Integer> numStack = new Stack<>();
        // 字母栈
        Stack<String> stringStack = new Stack<>();

        int num = 0;
        String curString = "";
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if(Character.isDigit(c)){
                num = num*10+c-'0';
            }else if(c=='['){
                numStack.push(num);
                stringStack.push(curString);
                // 重置
                num = 0;
                curString="";
            }else if(c==']'){
                int n = numStack.pop();
                StringBuilder stringBuilder = new StringBuilder();
                for(int j = 0;j<n;j++){
                    stringBuilder.append(curString);
                }
                curString = stringBuilder.toString();
            }else {
                curString+=c;
            }
        }
        return curString;
    }
}
